﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pictureAnalyse
{
    class ValueAnalyser
    {
        #region 颜色值统计信息渐变处理

        ///// <summary>
        ///// 对颜色统计值进行渐变处理，取V(i) = [V(i-1) + V(i) + V(i+1)] / 3
        ///// </summary>
        //public float[] arverageProcess(float[] V)
        //{
        //    float[] F = new float[V.Length];

        //    //float S = 0, E = 0;
        //    //for (int i = 0; i < V.Length; i++)
        //    //{
        //    //    S = (i - 1 >= 0) ? V[i - 1] : 0;        // V(i-1)
        //    //    E = (i + 1 < V.Length) ? V[i + 1] : 0;  // V(i+1)
        //    //    F[i] = (S + V[i] + E) / 3;              // 取均值
        //    //}

        //    return F;
        //}

        /// <summary>
        /// 对V进行临近均值计算
        /// </summary>
        public static float[] getAverage(float[] V)
        {
            int range = getMaxRange(V.Length);      // 获取最大均值半径
            float[] F = new float[V.Length];

            for (int R = 1; R <= range; R++)        // 均值半径从1到maxR
            {
                F = new float[V.Length];
                for (int i = 0; i < V.Length; i++)  // 对所有值分别计算临近均值
                {
                    F[i] = Arverage(V, i, R);       // 计算第i项的R半径范围内的均值
                }
                V = F;
            }
            return F;
        }

        /// <summary>
        /// 获取相对于len的最大均值半径
        /// </summary>
        private static int getMaxRange(int len)
        {
            int R = 3, i = 1;
            while (R < len / 2)
            {
                R *= 2;
                i++;
            }
            return i;
        }

        /// <summary>
        /// 获取index项前后range项范围内的均值
        /// </summary>
        private static float Arverage(float[] V, int index, int range)
        {
            float sum = I(V, index);
            for (int i = 1; i <= range; i++)
            {
                sum += I(V, index - i);
                sum += I(V, index + i);
            }
            return sum / (range * 2 + 1);   // 取均值
        }

        /// <summary>
        /// 获取V的第index项值
        /// </summary>
        private static float I(float[] V, int index)
        {
            if (V == null || V.Length == 0 || index < 0 || index >= V.Length) return 0;
            else return V[index];
        }

        #endregion

        
    }
}
